package com.fingard.dsp.bank.directbank.cmbunion02;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.ICloseOrder;
import com.fingard.dsp.bank.directbank.cmbunion02.utils.SignatureUtil;
import com.fingard.dsp.bank.reqrespobj.closeorder.CloseOrderState;
import com.fingard.dsp.bank.reqrespobj.closeorder.ReqCloseOrder;
import com.fingard.dsp.bank.reqrespobj.closeorder.RetAtsCloseOrder;

import java.util.HashMap;
import java.util.Map;

public class CMBUnion02CloseOrder extends CMBUnion02Base implements ICloseOrder {
    @Override
    public RetAtsCloseOrder closeOrder(ReqCloseOrder p_req) {
        RetAtsCloseOrder retAts = new RetAtsCloseOrder(p_req.reqHeader);
        retAts.rdSeq = p_req.rdSeq;
        try {
            ActSetItem actItem = this.getBankConfig().getActSetItem(
                    p_req.ownAct, "");
            if (actItem != null) {
                Map<String, String> requestPublicParams = getCommonMap();
                Map<String, Object> order = new HashMap<String, Object>();
                order.put("merId", actItem.merchantId);
                order.put("origOrderId", p_req.rdSeq);
                order.put("userId", actItem.userCode);
                WriteBankUrlLogLn("招商聚合扫码下单地址："+bankFront.serverURL+"/close");
                ObjectMapper mapper = new ObjectMapper();
                requestPublicParams.put("biz_content", mapper.writeValueAsString(order));
                WriteBankLogLn("招商聚合加签前的报文内容：" + mapper.writeValueAsString(requestPublicParams));
                String signContent= SignatureUtil.getSignContent(requestPublicParams);
                String sign = SignatureUtil.rsaSign(signContent, actItem.ownKeyStorePath, "UTF-8");
                requestPublicParams.put("sign", sign);
                String[] tmpStrRet = sendToBank(sign,mapper.writeValueAsString(requestPublicParams),bankFront.serverURL+"/close",actItem);
                if(tmpStrRet[0].length() == 0){
                    JSONObject resultObj = JSONObject.parseObject(tmpStrRet[1]);
                    if(resultObj.get("returnCode")!=null && "SUCCESS".equals(resultObj.get("returnCode"))){
                        String  respCode = resultObj.get("respCode").toString();
                        JSONObject biz_content = JSONObject.parseObject(resultObj.getString("biz_content"));
                        if(respCode!=null && "SUCCESS".equals(respCode)){
                            String closeState = biz_content.get("closeState").toString();
                            if("C".equals(closeState)){
                                retAts.closeState = CloseOrderState.Close_Success;
                            }else{
                                retAts.closeState = CloseOrderState.Close_Fail;
                            }
                            retAts.rdSeq = p_req.rdSeq;
                            retAts.respCode = "0";
                            retAts.respInfo = "";
                        }else {
                            retAts.respCode = "-1";
                            retAts.respInfo = resultObj.get("respMsg").toString();
                            if(resultObj.getString("errCode").equals("CMBORDERID_NOT_EXIST")){
                                retAts.rdSeq = p_req.rdSeq;
                                retAts.respCode = "0";
                                retAts.closeState = CloseOrderState.Close_Success;
                                retAts.respInfo = "关单成功";
                            }
                        }
                    }else {
                        retAts.respCode = "-1";
                        retAts.respInfo = resultObj.getString("respMsg");
                    }
                }
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = "未找到账号:[" + p_req.ownAct + "]相关商户配置信息!";
            }
        } catch (Exception ex) {
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            ex.printStackTrace();
            WriteBankLogLn(ex);
        }
        return retAts;
    }
}
